const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const normalAxios = axios.create();
const mockAxios = axios.create();
import url_config from "./config";
import {jsonToTreeByRoot} from "../utils/tools";
import reqTools from "../utils/reqTools";

// mock 数据  超管 广告机
var mock = new MockAdapter(mockAxios);

mock.onPost('/addHard').reply((config) => {
  return new Promise(function(resolve, reject) { 
    reqTools.post('/api/hardInfo', config.data).then((res) => {
     resolve([200, res ]);
   }).catch((err) => {
     resolve([500, err ]);
   });
 });
});

mock.onGet('/queryAll').reply((config) => {
  config.type="14"
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/hardInfo',
    config).then((res) => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });

});

mock.onDelete('/deleteHard').reply((config) => {
  /* return new Promise(function(resolve, reject) {
    reqTools.delete( '/api/sysCnotice/'+config.id).then((res) => {
      resolve([200, res ]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  }); */

  return new Promise(function(resolve, reject) {
    const ids= config.ids;
    reqTools.delete('/api/hardInfo/deletes',{ids}).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});

mock.onPut('/updateHard').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.put( '/api/hardInfo',config.data,
     ).then((res) => {
      resolve([200, res ]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});


/* mock.onGet('/getAreaByParent').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/sysArea/findByParent?parentId='+config.id).then(res => {
      console.log("这是api")
      console.log(res.data)
      for(let i=0;i<res.data.length;i++){
        res.data[i].value = res.data[i].id;
        res.data[i].label = res.data[i].name;
        res.data[i].key = res.data[i].id;
      }
      console.log(jsonToTreeByRoot({
        json:res.data,
        idKey: "id",
        pidKey: "parentId",
        childKey: "children",
        rootId:0,
        validate: row => {
          return true;
        }
      }));
      resolve([200,jsonToTreeByRoot({
        json:res.data,
        idKey: "id",
        pidKey: "parentId",
        childKey: "children",
        rootId:0,
        validate: row => {
          return true;
        }
      })]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
}); */


mock.onGet('/sysUser/getRegisterTargetCount').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/communityAcoust/getRegisterTargetCount',
    config).then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
export default mockAxios;
